package com.wn.pettyloan.controller;

import com.wn.pettyloan.entity.Contract;
import com.wn.pettyloan.entity.ContractTemplate;
import com.wn.pettyloan.entity.Debit;
import com.wn.pettyloan.service.ContractService;
import com.wn.pettyloan.service.DebitService;
import com.wn.pettyloan.service.RepayService;
import com.wn.pettyloan.util.Result;
import com.wn.pettyloan.vo.HistogramVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: pettyloan 合同表
 * @ClassName ContractTemplateControllerr
 * @description: 合同表的crud
 * @author: 国雨浓
 * @create: 2022-12-07 15:22
 **/
@Api(tags = "合同相关")
@RestController
@RequestMapping("contract")
public class ContractController {
    @Resource
    private ContractService contractService;

    @Autowired
    private DebitService debitService;
    @Autowired
    private RepayService repayService;


    @ApiOperation("根据合同模板展示合同")
    @PostMapping("/new")
    public Result getNewContract(Integer userId) {
        //得到新合同的service放到了contractService里。
        //UserId到借款表找(状态为21)proId,再到合同模板表找contract_template_content，
        ContractTemplate contractTemplate = contractService.getNewContractByUserId(userId, 21);
        //这里是否还需查询期数返回
        return Result.ok().data("contractTemplate", contractTemplate);
    }

    @ApiOperation("签订合同")
    @PostMapping("/signed")
    public Result signedContract(@RequestBody Contract contract) {
        //生成合同表
        contractService.insertAndGetPK(contract);

        //设置之后可以获取自增id
        Integer contractId = contract.getContractId();
        //借款表更新
        Integer debitId = contract.getDebitId();
//        debitService.updateStatusById(debitId,31);
        Debit debit = debitService.selectByPrimaryKey(Long.parseLong(debitId.toString()));

        debit.setDebitStatus(31);
        debit.setContractId(contractId);
        debitService.updateByPrimaryKeySelective(debit);

        //生成还款表,调用生成还款表API
        repayService.addRepay(contractId);

        return Result.ok();
    }

    /*
     * @Author 国雨浓
     * @Description  添加合同
     * @Date 14:22 2022/12/8
     * @Param
     * @return
     **/
    @PostMapping("/insertSelective")
    public Result insertSelective(@RequestBody Contract contract) {
        int i = contractService.insertSelective(contract);
        if (i > 0) {
            return Result.ok();
        } else {
            return Result.error("");
        }
    }

    /*
     * @Author 国雨浓
     * @Description  根据id删除合同
     * @Date 14:24 2022/12/8
     * @Param
     * @return
     **/
    @DeleteMapping("{id}")
    public Result deleteContract(@PathVariable Long id) {
        contractService.deleteByPrimaryKey(id);
        return Result.ok();
    }

    /*
     * @Author 国雨浓
     * @Description  查询全部合同
     * @Date 14:27 2022/12/8
     * @Param
     * @return
     **/
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Contract> contracts = contractService.selectAll();
        return Result.ok().data("contracts", contracts);
    }

    /*
     * @Author 国雨浓
     * @Description  根据id查询合同
     * @Date 14:30 2022/12/8
     * @Param
     * @return
     **/
    @GetMapping("{id}")
    public Result selectByPrimaryKey(@PathVariable Long id) {
        Contract contract = contractService.selectByPrimaryKey(id);
        return Result.ok().data("contract", contract);
    }

    /*
     * @Author 国雨浓
     * @Description  根据json对象修改合同
     * @Date 14:32 2022/12/8
     * @Param
     * @return
     **/
    @PutMapping("")
    public Result updateByPrimaryKeySelective(@RequestBody Contract contract) {
        contractService.updateByPrimaryKeySelective(contract);
        return Result.ok();
    }

    @GetMapping("histogram")
    public Result histogram() {
        /** @Author 汪江
         * @Description histogram
         * @Date 10:42 2022/12/14
         * @Param []
         * @return com.wn.pettyloan.util.Result
         **/
        List<HistogramVo> histogram = contractService.histogram();
        List<String> list1 = new LinkedList<>();
        List<Integer> list2 = new LinkedList<>();
        for (HistogramVo h : histogram
        ) {
            list1.add(h.getProductName());
            list2.add(h.getCount());
        }
        return Result.ok().data("list1", list1).data("list2", list2);
    }
}


